package com.yilin.medical.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.yilin.medical.R;
import com.yilin.medical.fragment.MessageFragment;
import com.yilin.medical.fragment.MoreFragment;
import com.yilin.medical.fragment.PersonalCenterFragment;
import com.yilin.medical.views.BadgeView;

/**
 * 个人中心
 * 
 * @author lijianqun
 * 
 */
public class PersonalCenterActivity extends BaseActivity implements
		OnPageChangeListener {

	private ImageView mImageViewPersonalCenter,
			mImageViewMessage, mImageViewMore;
	private TextView mTextViewPersonalCenter, mTextViewMessage,
			mTextViewMore;
	private LinearLayout mLinearLayoutPersonalCenter, mLinearLayoutMessage,
			mLinearLayoutMore;
	private RelativeLayout mRelativeLayoutBadgeView;
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mDatas;
	private BadgeView mBadgeView;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentViewWithTitle(R.layout.activity_personalcenter);
		context = this;
		initView();
		initData();
		registerLintener();
	}

	private void initView() {
		mTitle.getTitle().setText("个人中心");
		mTextViewPersonalCenter = (TextView) findViewById(R.id.tv_personalcenter);
		mTextViewMessage = (TextView) findViewById(R.id.tv_message);
		mTextViewMore = (TextView) findViewById(R.id.tv_more);
		mImageViewPersonalCenter = (ImageView) findViewById(R.id.iv_personalcenter);
		mImageViewMessage = (ImageView) findViewById(R.id.iv_message);
		mImageViewMore = (ImageView) findViewById(R.id.iv_more);
		mLinearLayoutPersonalCenter = (LinearLayout) findViewById(R.id.ll_personalcenter);
		mLinearLayoutMessage = (LinearLayout) findViewById(R.id.ll_message);
		mLinearLayoutMore = (LinearLayout) findViewById(R.id.ll_more);
		mRelativeLayoutBadgeView = (RelativeLayout) findViewById(R.id.ll_BadgeView);
		mViewPager = (ViewPager) findViewById(R.id.vipager);
	}

	private void registerLintener() {
		mViewPager.setOnPageChangeListener(this);
		mLinearLayoutPersonalCenter.setOnClickListener(this);
		mLinearLayoutMessage.setOnClickListener(this);
		mLinearLayoutMore.setOnClickListener(this);
	}

	private void initData() {
		mDatas = new ArrayList<Fragment>();

		PersonalCenterFragment tab01 = new PersonalCenterFragment();
		MessageFragment tab02 = new MessageFragment();
		MoreFragment tab03 = new MoreFragment();

		mDatas.add(tab01);
		mDatas.add(tab02);
		mDatas.add(tab03);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public int getCount() {
				return mDatas.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return mDatas.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.ll_personalcenter:
			mViewPager.setCurrentItem(0, true);
			break;
		case R.id.ll_message:
			mViewPager.setCurrentItem(1, true);
			break;
		case R.id.ll_more:
			mViewPager.setCurrentItem(2, true);
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int position) {
		resetTextView();
		switch (position) {
		case 0:
			
			if (mBadgeView != null) {
				mRelativeLayoutBadgeView.removeView(mBadgeView);
			}
			mBadgeView = new BadgeView(context);
			mBadgeView.setBadgeCount(7);
//			mBadgeView.setGravity(Gravity.RIGHT|Gravity.TOP);
			mRelativeLayoutBadgeView.addView(mBadgeView);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
			layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			mBadgeView.setLayoutParams(layoutParams);
			mTitle.getTitle().setText("个人中心");
			mTextViewPersonalCenter.setTextColor(Color.parseColor("#2B95D3"));
			mImageViewPersonalCenter.setImageResource(R.drawable.person_light);
			break;
		case 1:
			mTitle.getTitle().setText("我的消息");
			mTextViewMessage.setTextColor(Color.parseColor("#2B95D3"));
			mImageViewMessage.setImageResource(R.drawable.message_light);
			break;
		case 2:
			mTitle.getTitle().setText("更多");
			mTextViewMore.setTextColor(Color.parseColor("#2B95D3"));
			mImageViewMore.setImageResource(R.drawable.more_light);
			break;
		}

	}

	private void resetTextView() {// #2B95D3蓝色
		mTextViewPersonalCenter.setTextColor(Color.parseColor("#BEB8B8"));
		mImageViewPersonalCenter.setImageResource(R.drawable.person_gray);

		mTextViewMessage.setTextColor(Color.parseColor("#BEB8B8"));
		mImageViewMessage.setImageResource(R.drawable.message_gray);

		mTextViewMore.setTextColor(Color.parseColor("#BEB8B8"));
		mImageViewMore.setImageResource(R.drawable.more_gray);
	}

}
